//
//  JwProgressHUD.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-21.
//

#import "JwProgressHUD.h"
#import "JwCore.h"
#import "JwCate.h"

NSString *GV_JwProgressHUD_backColorString = @"FFFFFF";
NSString *GV_JwProgressHUD_tintColorString = @"000000";

CGFloat GV_JwProgressHUD_delay = 1.5;

NSString *GV_JwProgressHUD_successImageName = @"jw_hub_success";
NSString *GV_JwProgressHUD_warnImageName = @"jw_hub_warn";
NSString *GV_JwProgressHUD_errorImageName = @"jw_hub_error";

@implementation JwProgressHUD

+ (JwProgressHUD *)showHUDTo:(nullable UIView *)view
                  customView:(nullable UIView *)customView
                        text:(nullable NSString *)text
                       model:(MBProgressHUDMode)model
                       delay:(NSTimeInterval)delay {
    if (view == nil) {
        view = [JwScreen window];
    }
    JwProgressHUD *hud = [JwProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = model;
    if (customView) {
        hud.customView = customView;
    }
    if (text) {
        hud.detailsLabel.font = [UIFont systemFontOfSize:15];
        hud.detailsLabel.text = text;
    }
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [UIColor jw_colorHexString:GV_JwProgressHUD_backColorString];
    hud.contentColor = [UIColor jw_colorHexString:GV_JwProgressHUD_tintColorString];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.removeFromSuperViewOnHide = YES;
    [hud showAnimated:YES];
    if (delay > 0) {
        [hud hideAnimated:YES afterDelay:delay];
    }
    return hud;
}

+ (JwProgressHUD *)showHUDTo:(nullable UIView *)view text:(nullable NSString *)text {
    return [self showHUDTo:view customView:nil text:text model:(MBProgressHUDModeIndeterminate) delay:0];
}

+ (JwProgressHUD *)showHUDText:(nullable NSString *)text {
    return [self showHUDTo:nil text:text];
}

+ (JwProgressHUD *)showHUDTo:(nullable UIView *)view {
    return [self showHUDTo:view text:nil];
}

+ (JwProgressHUD *)showHUD {
    return [self showHUDTo:nil];
}

+ (JwProgressHUD *)showCustomHUDTo:(nullable UIView *)view customView:(nullable UIView *)customView text:(nullable NSString *)text delay:(NSTimeInterval)delay {
    return [self showHUDTo:view customView:customView text:text model:(MBProgressHUDModeCustomView) delay:delay];
}

+ (JwProgressHUD *)showCustomHUDTo:(nullable UIView *)view image:(nullable UIImage *)image text:(nullable NSString *)text delay:(NSTimeInterval)delay {
    UIImageView *imageView = nil;
    if (image) {
        imageView = [[UIImageView alloc] initWithImage:image];
    }
    return [self showHUDTo:nil customView:imageView text:text model:(MBProgressHUDModeCustomView) delay:delay];
}

+ (JwProgressHUD *)showCustomHUDTo:(nullable UIView *)view image:(nullable UIImage *)image text:(nullable NSString *)text {
    return [self showCustomHUDTo:view image:image text:text delay:GV_JwProgressHUD_delay];
}

+ (JwProgressHUD *)showSuccess:(nullable NSString *)text toView:(nullable UIView *)view {
    UIImage *image = [[UIImage imageNamed:GV_JwProgressHUD_successImageName] 
                      jw_imageByTintColor:kJwColorHexString(GV_JwProgressHUD_tintColorString)];
    return [self showCustomHUDTo:view image:image text:text];
}

+ (JwProgressHUD *)showWarning:(nullable NSString *)text toView:(nullable UIView *)view {
    UIImage *image = [[UIImage imageNamed:GV_JwProgressHUD_warnImageName]
                      jw_imageByTintColor:kJwColorHexString(GV_JwProgressHUD_tintColorString)];
    return [self showCustomHUDTo:view image:image text:text];
}

+ (JwProgressHUD *)showError:(nullable NSString *)text toView:(nullable UIView *)view {
    UIImage *image = [[UIImage imageNamed:GV_JwProgressHUD_errorImageName]
                      jw_imageByTintColor:kJwColorHexString(GV_JwProgressHUD_tintColorString)];
    return [self showCustomHUDTo:view image:image text:text];
}

+ (void)showError:(nullable NSString *)text {
    [self showError:text toView:nil];
}

+ (void)showSuccess:(nullable NSString *)text {
    [self showSuccess:text toView:nil];
}

+ (void)showWarning:(nullable NSString *)text {
    [self showWarning:text toView:nil];
}

+ (JwProgressHUD *)showTextHUDTo:(nullable UIView *)view text:(nullable NSString *)text delay:(NSTimeInterval)delay {
    return [self showHUDTo:view customView:nil text:text model:(MBProgressHUDModeText) delay:delay];
}

+ (JwProgressHUD *)showTextHUDTo:(nullable UIView *)view text:(nullable NSString *)text {
    return [self showHUDTo:view customView:nil text:text model:(MBProgressHUDModeText) delay:GV_JwProgressHUD_delay];
}

+ (JwProgressHUD *)showText:(nullable NSString *)text {
    return [self showTextHUDTo:nil text:text];
}

@end
